package com.kgc.controller;

import com.kgc.entity.Role;
import com.kgc.service.RoleService;
import com.kgc.utils.ResultMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role";
    }

    @RequestMapping("/roleAll")
    @ResponseBody
    public ResultMapUtils getRoleAll(){
        return new ResultMapUtils("0","操作成功",roleService.getRoleList(0));
    }


    @RequestMapping("/edit_role")
    public String editRolePage( Integer pageType,Integer roleId, Model model){
        Role role=new Role();
        if (pageType==1){
            //修改  先查询 再修改
            role=roleService.getRoleById(roleId);
        }
        model.addAttribute("role",role);
        return "edit_role";
    }

    @RequestMapping("/saveRole")
    @ResponseBody
    public ResultMapUtils saveRole(Role role){
        if (role==null){
            return ResultMapUtils.ERROR("r-1","参数错误");
        }
        if (roleService.saveRole(role)){
            return ResultMapUtils.SUCCESS("操作成功");
        }else {
            return ResultMapUtils.ERROR("r-2","系统错误！");
        }

    }


    @RequestMapping("/onOffRoleStatus")
    @ResponseBody
    public ResultMapUtils onOffRoleStatus(Integer status,Integer id){
        //做角色的删除操作
        int num=roleService.updateRoleStatus(status,id);
        if (num>0){
            return ResultMapUtils.SUCCESS("操作成功");
        }else {
            return ResultMapUtils.ERROR("rm-1","修改失败");
        }
    }


    @RequestMapping("/del_role")
    @ResponseBody
    public ResultMapUtils delRole(Role[] roleList){
        if (roleList.length>0){
            roleService.delRole(roleList);
            return ResultMapUtils.SUCCESS("操作成功");
        }else {
            return ResultMapUtils.ERROR("rm-2","参数异常");
        }
    }


    @RequestMapping("/countRoleByName")
    @ResponseBody
    public ResultMapUtils countRoleByName(String roleName){
        if (roleName==null||roleName==""){
            return ResultMapUtils.ERROR("addRole-1","角色名字不能为空");
        }

        int count=roleService.countRoleByName(roleName);
        if (count>0){
            return ResultMapUtils.ERROR("addRole-2","此角色名称已存在，请换一个");
        }else {
            return ResultMapUtils.SUCCESS("此角色名可用");
        }
    }


    @RequestMapping("/goShouQuanPage")
    public String goShouQuanPage(Integer roleId,Model model){
        model.addAttribute("roleId",roleId);
        return "role_shouquan";
    }

    @RequestMapping("/update_roleMenu")
    @ResponseBody
    public ResultMapUtils updateRoleMenu(String checkedIds,Integer roleId){
        if (roleId==null){
            return ResultMapUtils.ERROR("uprm-1","参数异常");
        }
        int num=roleService.updateRoleMenu(checkedIds,roleId);
        if (num>0){
            return ResultMapUtils.SUCCESS("更新成功");
        }else {
            return ResultMapUtils.ERROR("uprm-2","更新失败");
        }
    }
}
